// logout.c
// Session Test Program For TAMS Management
//
// By LHX 1999-8-3
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "mysql.h"
#include "cgi-lib.h"
#include "cgi-llist.h"
#include "html-lib.h"
#include "string-lib.h"

#include "tams_comm.h"
#include "tlib_all.h"

#define	USER_ID_LEN						20
#define	PASSWD_LEN						20

#define	ACTION_LEN						10

#define	ERROR_MESSAGE_LEN				2048
#define	MAX_RETURN_LINE_NUM			50
#define	ERROR_TEMPLATE_FILE			"/usr/local/tips_stcall/htdocs/session/error.htm"
#define	TEMPLATE_FILE					"/usr/local/tips_stcall/htdocs/session/index.html"

int CheckLogout(MYSQL *pmysql, char *sErrorMessage)
{
	if (TLib_Session_Clear() != 0)
	{
		strcpy(sErrorMessage, "Fail To Clear Session");
		return -101;
	}
		
	return 0;
}

int SendHTMLOutput(char *sResultTemplateFile)
{
	char 	*html;
	int i, iIsEditable;

	if (TLib_Session_Header(10) != 0)
	{
		printf("Fail To Send Session Header");
		return -402;
	}

	html = TLib_Tools_HtmlLoad(sResultTemplateFile);
	if (html == NULL) 
	{
		printf("Fail To Load HTML Template File");
		return -401;
	}

	TLib_Tools_HtmlFile(html, "", "");
	TLib_Tools_HtmlFree(html);

	return 0;
}
							
//////////////////////////////////////////////////////////////////////////////

int Init(MYSQL *pmysql, char *sErrorMessage)
{
	if (mysql_connect(pmysql, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD) == NULL)
	{
		sprintf(sErrorMessage, "Fail To Connect To DB Server");
		mysql_close(pmysql);
		return -101;
   }

   if (mysql_select_db(pmysql, MYSQL_DB) < 0)
   {
		sprintf(sErrorMessage, "Fail To Select Database\n");
      mysql_close(pmysql);
      return -102;
   }

	return 0;
}

void Terminate(MYSQL *pmysql)
{
	mysql_close(pmysql);
}

void SendErrorMessage(char *sErrorMessage, char *sErrorTemplateFile)
{
	char 	*html;

	if (TLib_Session_Header(-1) != 0)
	{
		printf("Fail To Send Session Header");
		return;
	}
		

	html = TLib_Tools_HtmlLoad(sErrorTemplateFile);
	if (html == NULL) 
	{
		printf("Fail To Load HTML Template File");
		return;
	}

	TLib_Tools_HtmlFile(html, "", "$msg_begin");
	printf(sErrorMessage);
   TLib_Tools_HtmlFile(html, "$msg_end", "");

	free(html);
}

int main()
{
	int iReturnCode;
	char sErrorMessage[ERROR_MESSAGE_LEN+1];

	MYSQL mysql;
	int iIsLoginSuccess;
	
	iReturnCode = Init(&mysql, sErrorMessage);
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		return iReturnCode;
	}
	
	iReturnCode = CheckLogout(&mysql, sErrorMessage);
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		Terminate(&mysql);
		return iReturnCode;
	}
		
	iReturnCode = SendHTMLOutput(TEMPLATE_FILE);
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		Terminate(&mysql);
		return iReturnCode;
	}

	Terminate(&mysql);
	return 0;
}
